package com.micangcaihang.micang.http;

import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpMethod;
import com.lzy.okgo.model.HttpParams;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.okgo.RxUtils;
import com.micangcaihang.common.utils.MapBeanConvert;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.entity.ServiceExmaineEntity;
import com.micangcaihang.micang.entity.TaskAccessoryInfoEntity;
import com.micangcaihang.micang.entity.TaskAssessResultEntity;
import com.micangcaihang.micang.entity.TaskLandInfoEntity;
import com.micangcaihang.micang.entity.TaskListEntity;
import com.micangcaihang.micang.entity.TaskPriceSurveyEntity;
import com.micangcaihang.micang.entity.TaskRimMatingEntity;
import com.micangcaihang.micang.entity.TaskTuftMatingEntity;
import com.micangcaihang.micang.entity.TaskWayStatusEntity;
import com.micangcaihang.micang.entity.TuftInfoEntity;
import com.micangcaihang.micang.entity.UserEntity;
import com.micangcaihang.micang.entity.UserInfoSp;
import com.micangcaihang.micang.http.request.TaskAssessResultRequest;
import com.micangcaihang.micang.http.request.TaskLandInfoRequest;
import com.micangcaihang.micang.http.request.TaskRimMatingRequest;
import com.micangcaihang.micang.http.request.TaskTuftMatingRequest;
import com.micangcaihang.micang.http.request.TaskTuftRequest;
import com.micangcaihang.micang.http.request.TaskWayStatusRequest;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/08/02
 *     update : 2017/08/02
 *     desc   : 服务器信息
 *     version: 1.0
 * </pre>
 */

public class ApiService {
    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @param <T>
     * @return
     */
    public static <T> Observable<T> user_logon(String username, String password) {
        Type type = new TypeToken<ApiResult<UserEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("username", username);
        params.put("password", password);
        return RxUtils.request(HttpMethod.POST, ApiConstant.USER_LOGIN, type, params);
    }

    /**
     * 调评 - 01获取待处理列表
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_pending() {
        Type type = new TypeToken<ApiResult<List<TaskListEntity>>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("userid", UserInfoSp.getInstance().getUserid());
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_PENDING, type, params);
    }

    /**
     * 调评 - 02调评专员退回任务列表
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_reback() {
        Type type = new TypeToken<ApiResult<List<TaskListEntity>>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("userid", UserInfoSp.getInstance().getUserid());
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_REBACK, type, params);
    }

    /**
     * 调评 - 03调评专员表单-房产信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_houseproperty(String id) {
        Type type = new TypeToken<ApiResult<TuftInfoEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_HOUSEPROPERTY, type, params);
    }

    /**
     * 调评 - 03调评专员表单-房屋配套
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_matching(String id) {
        Type type = new TypeToken<ApiResult<TaskTuftMatingEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_MATCHING, type, params);
    }

    /**
     * 调评 - 03调评专员表单-道路状况
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_road(String id) {
        Type type = new TypeToken<ApiResult<TaskWayStatusEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_ROAD, type, params);
    }

    /**
     * Diao_Ping - 12 道路状况修改与添加
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_roadupdate(TaskWayStatusRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_ROADUPDATE, type, params);
    }

    /**
     * 调评 - 03调评专员表单-周边配套
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_supporting(String id) {
        Type type = new TypeToken<ApiResult<TaskRimMatingEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_SUPPORTING, type, params);
    }

    /**
     * 调评 - 03调评专员表单-周边配套
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_supportingupdate(TaskRimMatingRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        List<TaskRimMatingRequest.SupportIdBean> business_matching = request.getBusiness_matching();
        List<TaskRimMatingRequest.SupportIdBean> life_support = request.getLife_support();
        List<TaskRimMatingRequest.SupportIdBean> other_support = request.getOther_support();
        for (int i = 0; i < business_matching.size(); i++) {
            params.put("business_matching[" + (i + 1) + "]", business_matching.get(i).getSupport_id());
        }
        for (int i = 0; i < life_support.size(); i++) {
            params.put("life_support[" + (i + 1) + "]", life_support.get(i).getSupport_id());
        }
        for (int i = 0; i < other_support.size(); i++) {
            params.put("other_support[" + (i + 1) + "]", other_support.get(i).getSupport_id());
        }
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_SUPPORTINGUPDATE, type, params);
    }

    /**
     * 16新楼价格修改与添加
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_xinlouupdate(TaskPriceSurveyEntity request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        List<TaskPriceSurveyEntity.XinLou> xinlou = request.getXinlou();
        for (int i = 0; i < xinlou.size(); i++) {
            params.put("xinlou[" + (i + 1) + "][estimate_fee]", xinlou.get(i).getEstimate_fee());
            params.put("xinlou[" + (i + 1) + "][evaluate_id]", StringUtils.isEmpty(xinlou.get(i).getEvaluate_id()) ? "0" : xinlou.get(i).getEvaluate_id());
            params.put("xinlou[" + (i + 1) + "][reference_name]", xinlou.get(i).getReference_name());
            params.put("xinlou[" + (i + 1) + "][unit_price]", xinlou.get(i).getUnit_price());
        }
        params.put("id", request.getId());
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_XINLOUUPDATE, type, params);
    }

    /**
     * 17中介快速成交价修改与添加
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_zhongjieupdate(TaskPriceSurveyEntity request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        List<TaskPriceSurveyEntity.ZhongJie> zhongjie = request.getZhongjie();
        for (int i = 0; i < zhongjie.size(); i++) {
            params.put("zhongjie[" + (i + 1) + "][unit_price]", zhongjie.get(i).getUnit_price());
            params.put("zhongjie[" + (i + 1) + "][evaluate_id]", StringUtils.isEmpty(zhongjie.get(i).getEvaluate_id()) ? "0" : zhongjie.get(i).getEvaluate_id());
            params.put("zhongjie[" + (i + 1) + "][reference_name]", zhongjie.get(i).getReference_name());
            params.put("zhongjie[" + (i + 1) + "][contacts]", zhongjie.get(i).getContacts());
            params.put("zhongjie[" + (i + 1) + "][evaluate_source]", zhongjie.get(i).getEvaluate_source());
        }
        params.put("id", request.getId());
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_ZHONGJIEUPDATE, type, params);
    }

    /**
     * 调评 - 03调评专员表单-土地信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_land(String id) {
        Type type = new TypeToken<ApiResult<TaskLandInfoEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_LAND, type, params);
    }

    /**
     * 删除价格
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_pricesurveydelete(String id, String evaluate_id) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        params.put("evaluate_id", evaluate_id);
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_PRICESURVEYDELETE, type, params);
    }

    /**
     * Diao_Ping - 14 土地信息修改与添加
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_landupdate(TaskLandInfoRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_LANDUPDATE, type, params);
    }

    /**
     * 调评 - 03调评专员表单-价格调查
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_priceSurvey(String id) {
        Type type = new TypeToken<ApiResult<TaskPriceSurveyEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_PRICESURVEY, type, params);
    }

    /**
     * 调评 - 03调评专员表单-评估结果
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_assessment(String id) {
        Type type = new TypeToken<ApiResult<TaskAssessResultEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_ASSESSMENT, type, params);
    }

    /**
     * Diao_Ping - 14 土地信息修改与添加
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_assessmentupdate(TaskAssessResultRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_ASSESSMENTUPDATE, type, params);
    }

    /**
     * 调评 - 03调评专员表单-调评专员表单-信息附件
     *
     * @param <T>
     * @param file_type 附件页面编号 1 2 3
     * @return
     */
    public static <T> Observable<T> invest_attachment(String id, String file_type) {
        Type type = new TypeToken<ApiResult<List<TaskAccessoryInfoEntity>>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        params.put("file_type", file_type);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_ATTACHMENT, type, params);
    }

    /**
     * 调评 - 03调评专员表单-房屋配套提交
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_matchingupdate(TaskTuftMatingRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_MATCHINGUPDATE, type, params);
    }

    /**
     * 调评 - 03调评专员表单-房产信息保存
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_propertyupdate(TaskTuftRequest request) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        Map<String, String> map = MapBeanConvert.objectToMapString(request);
        HttpParams params = new HttpParams();
        params.put(map);
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_PROPERTYUPDATE, type, params);
    }

    /**
     * 调评 - 01获取下拉框选项数据
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> util_choose(String typechoose) {
        Type type = new TypeToken<ApiResult<List<PickerEntity>>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("type", typechoose);
        return RxUtils.request(HttpMethod.GET, ApiConstant.UTIL_CHOOSE, type, params, CacheMode.IF_NONE_CACHE_REQUEST, typechoose);
    }

    /**
     * Feng_Kong - 16 提交
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_submitorder(String id, String reback) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        params.put("reback", reback);
        params.put("userid", UserInfoSp.getInstance().getUserid());
        return RxUtils.request(HttpMethod.POST, ApiConstant.INVEST_SUBMITORDER, type, params);
    }

    /**
     * Diao_Ping - 5调评模块中资料审查调评信息
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> invest_datareview(String id) {
        Type type = new TypeToken<ApiResult<ServiceExmaineEntity>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.GET, ApiConstant.INVEST_DATAREVIEW, type, params);
    }

    /**
     * Feng_Kong - 16 提交
     *
     * @param <T>
     * @return
     */
    public static <T> Observable<T> util_delete(String id) {
        Type type = new TypeToken<ApiResult<Void>>() {
        }.getType();
        HttpParams params = new HttpParams();
        params.put("id", id);
        return RxUtils.request(HttpMethod.POST, ApiConstant.UTIL_DELETE, type, params);
    }


}
